package com.example.emos.api.controller;

import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.controller.form.*;
import com.example.emos.api.db.pojo.Tbtemplate;
import com.example.emos.api.service.TemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;

/**
 * @author ：Supyo
 * @date ：Created in 2021/11/15 13:26
 * @description：
 */
@RestController
@RequestMapping("/markTemplate")
@Tag(name = "TemplateController", description = "考核模板接口")
@Slf4j
public class TemplateController {
    @Autowired
    private TemplateService templateService;

    @PostMapping("/selectTemplateListById")
    @Operation(summary = "根据模板Id查询考核模板")
    public R selectTemplateListById(@Validated @RequestBody SelectTemplateListByIdForm form) {
        Tbtemplate template = templateService.selectTemplateListById(form.getId());
        return R.ok().put("data", template);
    }

    @PostMapping("/selectTemplateListBypages")
    @Operation(summary = "分页查询考核模板")
    public R selectTemplateListBypages(@Valid @RequestBody SelectTemplateListBypagesForm form) {
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("start", start);
        PageUtils pageUtils = templateService.selectTemplateListBypages(param);
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/addTemplate")
    @Operation(summary = "添加考核模板")
    public R insert(@Valid @RequestBody InsertTemplateForm form) {
        Tbtemplate template = JSONUtil.parse(form).toBean(Tbtemplate.class);
        template.setTitle(form.getTitle());
        template.setOptions(JSONUtil.parseArray(form.getOptions()).toString());

        template.setCreator("测试创建人员");
        template.setCreateTime(new Date());
        int rows = templateService.insert(template);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/updateTemplate")
    @Operation(summary = "修改考核模板")
    public R updateTemplate(@Valid @RequestBody UpdateTemplateForm form) {
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.replace("options", JSONUtil.parseArray(form.getOptions()).toString());
        int rows = templateService.updateTemplate(param);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/deleteTemplate")
    @Operation(summary = "删除考核模板记录")
    public R deleteTemplate(@Valid @RequestBody DeleteTemplateForm form) {
        int rows = templateService.deleteTemplateByIds(form.getIds());
        return R.ok().put("rows", rows);
    }
}
